package com.ran.dao.daoImpl;

import com.ran.dao.bookDao;
import com.ran.entity.book;
import com.ran.entity.employee;
import com.ran.utils.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class bookDaoImpl implements bookDao {
    @Override
    public List<book> getbooks(String text) {
        //准备数据
        Connection connect = jdbc.getconnect();
        String sql = "SELECT * FROM books WHERE text= ?";
        PreparedStatement statement=null;
        ResultSet result = null;
        ArrayList<book> list = new ArrayList<>();
        //执行sql语句
        try {
            statement=connect.prepareStatement(sql);
            statement.setString(1,text);
            result = statement.executeQuery();
            //处理数据
            while(result.next()){
                Integer bookid = result.getInt("bookid");
                String texts = result.getString("text");
                String author = result.getString("author");
                String publisher = result.getString("publisher");
                double price = result.getDouble("price");
                list.add(new book(bookid,texts,author,publisher,price));
            }
        } catch (SQLException e) {
            System.out.println("Query error in Dao!");
            e.printStackTrace();
        } finally {
            jdbc.realse(connect,statement,result);
        }
        return list;
    }
}
